package org.agony2d.view.puppet.supportClasses 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import org.agony2d.Agony;
	import org.agony2d.notify.AEvent;
	import org.agony2d.view.core.ComponentProxy;
	import org.agony2d.view.core.UIManager;
	
	import org.agony2d.core.agony_internal;
	use namespace agony_internal;
	
final public class ImagePuppetComp extends PuppetComp
{
	
	public function ImagePuppetComp()
	{
		m_img = new AutoSmoothingBitmap()
		this.addChild(m_img)
	}
	
	override public function set rotation( v:Number ) : void 
	{
		if (m_rotation != v)
		{
			super.rotation = v;
			if (m_rotation != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}
	
	override public function set scaleX( v:Number ) : void 
	{
		if (m_scaleX != v)
		{
			super.scaleX = v;
			if (m_scaleX != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}
	
	override public function set scaleY( v:Number ) : void 
	{
		if ( m_scaleY != v)
		{
			super.scaleY = v;
			if (m_scaleY != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}
	
	override agony_internal function makeTransform( smoothing:Boolean, external:Boolean ) : void
	{
		if (external)
		{
			m_externalTransform = smoothing
		}
		else
		{
			m_internalTransform = smoothing
		}
		m_img.smoothing = (m_internalTransform || m_externalTransform)
	}
	
	override agony_internal function dispose() : void
	{
		super.dispose()
		m_img.dispose()
		m_externalTransform = m_internalTransform = false
	}
	
	override agony_internal function recycle() : void
	{
		cachedImageList[cachedImageLength++] = this
	}
	
	agony_internal static function getImagePuppetComp( proxy:ComponentProxy ) : ImagePuppetComp
	{
		var img:ImagePuppetComp
		img = (cachedImageLength > 0 ? cachedImageLength-- : 0) ? cachedImageList.pop() : new ImagePuppetComp()
		img.m_proxy = proxy
		img.m_notifier.setTarget(proxy)
		return img
	}
	
	agony_internal static var cachedImageList:Array = []
	agony_internal static var cachedImageLength:int
	
	agony_internal var m_img:AutoSmoothingBitmap
	agony_internal var m_externalTransform:Boolean, m_internalTransform:Boolean
}
}